<%@page import="com.wh.minihelp.user.domain.ChildInfo"%>
<%@page import="com.wh.minihelp.user.util.UserUtil"%>
<%@page import="com.wh.minihelp.user.domain.FamilyAccount"%>
<%@page import="com.wh.minihelp.flower.domain.FamilyFlower"%>
<%@page import="java.util.List"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<%
	/* ----------------------------------------关联的家庭信息------------------------------------------- */
%>
<div class="column_title">
	<span>关联的家庭列表</span>
	<div class="column_title_oper">
		<a href="#" onclick="expand()">收起/展开</a>
	</div>
</div>
<div id="div_family" class="weui-cells weui-cells-ex">
	<%
		List<FamilyAccount> families = (List<FamilyAccount>) request.getAttribute("families");
		size = families.size();
		for (int i = 0; i < size; i++) {
			FamilyAccount ff = families.get(i);
	%>
	<cc data="<%=ff.getFamilyId()%>">
	<div class="weui-cell weui-cell-ex">
		<c><%=(i + 1)%></c>
		<d>.<%=(ff.isDefault() ? "[默认]" : "")%><%=ff.getFamily().getName()%></d>
	</div>
	<div class="weui-cell-oper">
		<a href="#" onclick="gotoFamily()">查看</a> &nbsp; <a href="#" onclick="leaveFamily()">退出</a> &nbsp; <a href="#" onclick="setDefaultFamily()">默认</a>
	</div>
	</cc>
	<%
		}
	%>
	<div class="weui-cells-oper">
		<a id="btnAddFamily" href="#" class="weui-btn weui-btn_mini weui-btn_primary">创建/加入新家庭</a>
	</div>
</div>
<div style='display: none'>
	<div style='display: none' id="div_addFamily_none">
		<div class="weui-cells weui-cells_form" id="div_addFamily">
			<div class="weui-cell" style='padding: 0px'>
				<div id='1' class="weui-navbar__item weui-bar__item_on">创建新家庭</div>
				<div id='2' class="weui-navbar__item">加入某家庭</div>
			</div>
			<div class="weui-cell">
				<div class="weui-cell__hd">
					<label class="weui-label">家庭名称:</label>
				</div>
				<div class="weui-cell__bd">
					<input class="weui-input" type="text" id="familyName" placeholder="一个好家庭，怎能没有靓名称~">
				</div>
			</div>
		</div>
	</div>
	<cc id="cc_family" data="">
	<div class="weui-cell weui-cell-ex">
		<c></c>
		<d></d>
	</div>
	<div class="weui-cell-oper">
		<a href="#" onclick="gotoFamily()">查看</a> &nbsp; <a href="#" onclick="leaveFamily()">退出</a> &nbsp; <a href="#" onclick="setDefaultFamily()">默认</a>
	</div>
	</cc>
</div>
<div style='padding: 5px;'></div>

<script type="text/javascript">
$(function(){
	$('#btnAddFamily').on('click', function(){
		$("#js_dialog_title").text("添加/加入新的家庭"); 
		$("#js_dialog_content").append($("#div_addFamily"));
		okFunction=addFamily;
		cancelFunction=cancelFamily;
		$androidDialog1.css('display','')
	});	
    $('#div_addFamily .weui-navbar__item').on('click', function () {
       	$(this).addClass('weui-bar__item_on ').siblings('.weui-bar__item_on').removeClass('weui-bar__item_on');
    });
});
	function addFamily(){
		var fname=$("#familyName").val();
		if ($.trim(fname).length == 0) {
			error("#familyName","一个好家庭，怎能没有靓名称，赶紧写一个吧！")
			return
		}
		if(!checkInput(fname)){
			error("#familyName","您的家庭名称太奇葩，我不认识，换一个吧！")
			return 
		}
		var ftype=$('#div_addFamily .weui-bar__item_on').attr('id');
		$.post("<%=path%>/u?m=family"
		  , { name: fname ,familyOper:ftype}	   
		  ,function(data){
				if(data.success){
					cancelFamily();
					var last=$("#div_family cc :last");
					var newLi=null;
					if(last.length==0){
						newLi=$("#cc_family").clone();
					}else{
						newLi=last.clone();
					}
					newLi.attr('data',data.value.familyId);
					newLi.find("d").text("."+data.value.family.name);
					if(last.length==0){
						newLi.insertBefore($("#div_family .weui-cells-oper"));
					}else{
						newLi.insertAfter(last);
					}
					resortCC("div_family");
				}else{
					alert(data.msg)
				}
		   }, "json");		
	}
	function gotoFamily(){
		var cur=$(event.srcElement).parents("cc");
		$.post("<%=path%>/u?m=GF", { id: cur.attr("data")}	   
		  ,function(data){
				if(data.success){
					window.location.href="<%=path%>/u?m=overview";
				}else{
					alert(data.msg)
				}
		   }, "json");	
	}
	function leaveFamily(){
		var cur=$(event.srcElement).parents("cc");
		$.post("<%=path%>/u?m=LF", { id: cur.attr("data")}	   
		  ,function(data){
				if(data.success){
					if(1==data.value){
						window.location.href='<%=path%>/u?m=mgr'
					}else{
						cur.remove();
						resortCC("div_family");
					}
				}else{
					alert(data.msg)
				}
		   }, "json");	
	}
	function setDefaultFamily(){
		var cur=$(event.srcElement).parents("cc");
		var data=cur.attr("data");
		// var cur=$(event.srcElement).parents("cc");
		$.post("<%=path%>/u?m=SDF", { id: cur.attr("data")}	   
		  ,function(data){
				if(data.success){
					window.location.href='<%=path%>/u?m=mgr'
				}else{
					alert(data.msg)
				}
		   }, "json");	
	}
	function cancelFamily(){
		$("#div_addFamily_none").append($("#div_addFamily"));
		$androidDialog1.css('display','none')
	}
</script>